﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AnalysisSystemLog
{
    /** 信息日志 **/
    public class LogRecord
    {
        // 编号
        public string innerID = String.Empty;
        public string InnerID
        {
            get { return innerID; }
            set
            {
                innerID = value;
            }
        }
        // 生成时间
        private string generatedTime = String.Empty;
        public string GeneratedTime
        {
            get { return generatedTime; }
            set 
            {
                generatedTime = value; 
            }
        }
        // 星期数据
        private string dayWeek = String.Empty;
        public string DayWeek
        {
            get { return dayWeek; }
            set
            {
                dayWeek = value;
            }
        }
        // 持续时间
        public string generatedDuration = String.Empty;
        public string GeneratedDuration
        {
            get { return generatedDuration; }
            set
            {
                generatedDuration = value;
            }
        }
        // 内容数据
        public string context = String.Empty;
        public string Context
        {
            get { return context; }
            set
            {
                context = value;
            }
        }
        // 评论内容
        private string comment = String.Empty;
        public string Comment
        {
            get { return comment; }
            set
            {
                comment = value;
            }
        }

        /**
          *  \brief 将日志转为字符串，以便写入文件
          *
          * \param recordData 待转换的结构
          * \returns 转换后的字符串
          */
        public static string ConvertRecordToString(LogRecord recordData)
        {
            return recordData.InnerID + "@" + recordData.GeneratedTime + "@" + recordData.DayWeek + "@" + recordData.GeneratedDuration + "@" + recordData.Context + "@" + "@" + recordData.Comment;    //倒数第二个是序号的占位符
        }

        /**
          *  \brief 将字符串转为日志数据
          *
          * \param recordStr 待转换的日志数据
          * \returns 转换后的日志数据
          */
        public static LogRecord ConvertStringToRecord(string recordStr)
        {
            LogRecord record = new LogRecord();
            string[] strArray = recordStr.Split(new string[] { "@" }, StringSplitOptions.None);
            //                 if (strArray.Length != 5)
            //                 {
            //                     MessageBox.Show("哪里没有把空项转成“”");
            //                     return s;
            //                 }

            record.InnerID = strArray[0];
            record.GeneratedTime = strArray[1];
            record.DayWeek = strArray[2];
            record.GeneratedDuration = strArray[3];
            record.Context = strArray[4];
            //5是序号，不用写入文件
            record.Comment = strArray.Length > 6 ? strArray[6] : "";
            return record;
        }

        /**
          *  \brief 将日志数据转为字符串数组
          *
          * \param recordStr 待转换的日志数据
          * \returns 转换后的字符串数组数据
          */
        public static string[] ConvertRecordToStringArray(LogRecord record)
        {
            string str = ConvertRecordToString(record);
            string[] strArray = str.Split(new string[] { "@" }, StringSplitOptions.None);
            return strArray;
        }


        /**
          *  \brief 将字符串转换为DateTime
          *
          * \param recordStr 字符串格式为2012-10-30 17:09:59
          * \returns 返回DateTime
          */
        public static DateTime ConvertStringToDateTime(string str)
        {
            string[] strArray = str.Split(' ');
            if (strArray.Length != 2)
            {
                return new DateTime(0);
            }

            string[] strArrayDate = strArray[0].Split('-');
            string[] strArrayTime = strArray[1].Split(':');

            DateTime dt = new DateTime();
            try
            {
                dt = new DateTime(Convert.ToInt32(strArrayDate[0]), Convert.ToInt32(strArrayDate[1]), Convert.ToInt32(strArrayDate[2]),
               Convert.ToInt32(strArrayTime[0]), Convert.ToInt32(strArrayTime[1]), Convert.ToInt32(strArrayTime[2]));
            }
            catch (System.Exception ex)
            {

            }
            return dt;
        }
    }
}
